# Downloads and installs a supported version of LuaJIT
cmake_minimum_required(VERSION 2.8)

# Add dependency for ExternalProject
include(ExternalProject)

# Download and compile SFML using an external project
ExternalProject_Add(luajit_EXTERNAL
    CMAKE_ARGS 
        -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
        -DBUILD_EXTRAS=NO
        -DBUILD_DEMOS=NO
        -DUSE_DOUBLE_PRECISION=ON
        -DBUILD_SHARED_LIBS=NO
        -DBUILD_STATIC_LIBS=ON
#    URL 
#        http://bullet.googlecode.com/files/bullet-2.79-rev2440.zip
    SVN_REPOSITORY
        http://bullet.googlecode.com/svn/trunk
)

# Recover project paths for additional settings
ExternalProject_Get_Property(luajit_EXTERNAL SOURCE_DIR BINARY_DIR INSTALL_DIR)

# Tell cmake that the external project generated a library so we 
# can add dependencies here instead of later 
add_library(luajit UNKNOWN IMPORTED) 
set_property(TARGET luajit 
  PROPERTY IMPORTED_LOCATION 
  ${INSTALL_DIR}/lib/libLuajitDynamics.a 
) 
add_dependencies(luajit luajit_EXTERNAL)

# Set properties as per FindModules
set(luajit_FOUND 
  TRUE
  CACHE INTERNAL "" FORCE 
)

set(luajit_INCLUDE_DIRS
  "${INSTALL_DIR}/include/luajit"
  CACHE INTERNAL "" FORCE
)

set(luajit_LIBRARIES
  "${INSTALL_DIR}/lib/libLuajitSoftBody.a"
  "${INSTALL_DIR}/lib/libLuajitDynamics.a"
  "${INSTALL_DIR}/lib/libLuajitCollision.a"
  "${INSTALL_DIR}/lib/libLinearMath.a"
  #    "-lLuajitSoftBody -lLuajitDynamics -lLuajitCollision -lLinearMath"
  CACHE INTERNAL "" FORCE
)

set(luajit_DEFINITIONS
  " -DBT_USE_DOUBLE_PRECISION "
  CACHE INTERNAL "" FORCE                                                    
)
